package com.newland.mesdk.demo.adapter;

import java.util.List;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.newland.mesdk.demo.main.R;

/**
 * 蓝牙搜索列表适配器
 * 
 * @ClassName BlueToothAdapter
 */
public class BlueToothBondAdapter extends BaseAdapter {
	private Context context;
	private int optionsGravity = Gravity.CENTER;
	private int xof = 0;
	private int yof = 0;
	private List<BluetoothDevice> bonddevices;
	private ListButtonClickListener listButtonClickListener;

	public BlueToothBondAdapter(Context context, List<BluetoothDevice> bonddevices) {
		this(context, bonddevices, Gravity.CENTER, 0, 0);
	}

	public BlueToothBondAdapter(Context context, List<BluetoothDevice> bonddevices, int optionsGravity, int xof, int yof) {
		this.context = context;
		this.bonddevices = bonddevices;
		this.optionsGravity = optionsGravity;
		this.xof = xof;
		this.yof = yof;
	}

	public void setListButtonClickListener(ListButtonClickListener listButtonClickListener) {
		this.listButtonClickListener = listButtonClickListener;
	}

	@Override
	public int getCount() {
		return bonddevices.size();
	}

	@Override
	public Object getItem(int position) {
		return bonddevices.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView != null) {
			viewHolder = (ViewHolder) convertView.getTag();
		} else {
			viewHolder = new ViewHolder();
			convertView = LinearLayout.inflate(context, R.layout.item_bondlist, null);
			viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_cancel);
			viewHolder.tv = (TextView) convertView.findViewById(R.id.tv_name);
			convertView.setTag(viewHolder);
		}
		viewHolder.tv.setText(bonddevices.get(position).getName());
		viewHolder.iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				listButtonClickListener.onItemButtonClick(position, bonddevices);
			}
		});
		return convertView;
	}

	private class ViewHolder {
		ImageView iv;
		TextView tv;
	}

}
